﻿#include "hntime.h"
#include "stream/hnstring.h"

HNTimer::HNTimer() 
{
    // 开始时间点
     start = clock::now();
}
void HNTimer::stop()
{
    // 结束时间点
     end = clock::now();
}
long long HNTimer::getCoastTime_ms() 
{
    stop();
    auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
    return duration.count();
}
std::string HNTimer::getCoastTime()
{
    long long size = getCoastTime_ms();
    if (size < 1000.0)
    {
        return size + " ms";
    }
    auto duration = std::chrono::duration_cast<std::chrono::seconds>(end - start);
    size = duration.count();
    if (size < 60)
    {
        return floatToStringWithTwoDecimalPlaces(size, 2) + " s";
    }
    duration = std::chrono::duration_cast<std::chrono::minutes>(end - start);
    size = duration.count();
    if (size < 60)
    {
        return floatToStringWithTwoDecimalPlaces(size, 2) + " min";
    }
    duration = std::chrono::duration_cast<std::chrono::hours>(end - start);
    size = duration.count();
    return floatToStringWithTwoDecimalPlaces(size, 2) + " h";

}
HNTimer::~HNTimer() 
{

}